import React from 'react';
import { Form, Input } from 'antd';
import { get as getPath } from 'object-path';
import { getDutyDetail2 } from '../../services';
import styles from './index.less';

const { TextArea } = Input;

const Post = ({ form, isAddPage, query, setLoading }) => {
    const { getFieldDecorator } = form;
    const [info, setInfo] = React.useState(null);

    const getDetail = async () => {
        setLoading(true);
        const res = await getDutyDetail2(query && query.dutyCode);
        const { data, code } = res;
        if (code === 10000) {
            // console.log(data);
            setInfo(data);
        }
        setLoading(false);
    };

    React.useEffect(() => {
        if (!isAddPage && query) {
            getDetail();
        }
    }, [query]);

    return (
        <Form className={styles.formWrap}>
            <Form.Item label="所属机构">
                {isAddPage ? getPath(query, 'organizationName', '') : getPath(info, 'organizationName', '')}
            </Form.Item>
            <Form.Item label="岗位名称(中文)">
                {getFieldDecorator('dutyAlias', {
                    initialValue: getPath(info, 'dutyAlias', ''),
                    rules: [
                        {
                            required: true,
                            message: '岗位名称(中文)不能为空',
                            whitespace: true
                        }
                    ]
                })(<Input placeholder="请输入岗位名称(中文)" allowClear maxLength={150} />)}
            </Form.Item>
            <Form.Item label="岗位名称(英文)">
                {getFieldDecorator('dutyAliasEnglish', {
                    initialValue: getPath(info, 'dutyAliasEnglish', '')
                })(<Input placeholder="请输入岗位名称(英文)" allowClear maxLength={150} />)}
            </Form.Item>
            <Form.Item label="岗位职责(中文)">
                {getFieldDecorator('dutyDesc', {
                    initialValue: getPath(info, 'dutyDesc', '')
                })(
                    <TextArea
                        placeholder="请输入岗位职责(中文)"
                        autosize={{ minRows: 3, maxRows: 6 }}
                        maxLength={800}
                    />
                )}
            </Form.Item>
            <Form.Item label="岗位职责(英文)">
                {getFieldDecorator('dutyDescEnglish', {
                    initialValue: getPath(info, 'dutyDescEnglish', '')
                })(
                    <TextArea
                        placeholder="请输入岗位职责(英文)"
                        autosize={{ minRows: 3, maxRows: 6 }}
                        maxLength={800}
                    />
                )}
            </Form.Item>
        </Form>
    );
};

export default Post;
